home *** CD-ROM | disk | FTP | other *** search
/ Explorers of the New World / Explorers of the New World.iso / pc / exdata.dir / 00725_Search.ls < prev    next >
Encoding:
Text File  |  1995-09-10  |  4.3 KB  |  151 lines

  1. on doFind input
  2.   global findButton
  3.   if isEnabled(findButton) then
  4.     clearDatabase()
  5.     activateButtonKeepActivated(findButton)
  6.     findUserTypedTopic(input)
  7.     enableButton(findButton)
  8.   end if
  9. end
  10.  
  11. on findUserTypedTopic input
  12.   global browserTopics, browserTopLine, numVisibleTopics, browserScroll
  13.   watchCursor()
  14.   set searchResults to doSearch(input)
  15.   normalCursor()
  16.   if not voidp(searchResults) and not (searchResults = EMPTY) then
  17.     set browserTopics to searchResults & RETURN
  18.     set browserTopLine to 1
  19.     setFieldText("browser", browserTopLine, numVisibleTopics, browserTopics)
  20.     moveScrollSquareToMatchText(browserTopLine, browserScroll)
  21.     setSearchSuccessFul(1)
  22.     selectTopic(1)
  23.     showSelectedTopic()
  24.   else
  25.     put "No topics found" into field "searchTopic"
  26.     set the selStart to 0
  27.     set the selEnd to 999
  28.     put EMPTY into field "browser"
  29.     unhiliteClickedTopic()
  30.     setSearchSuccessFul(0)
  31.   end if
  32. end
  33.  
  34. on showSelectedTopic
  35.   showTitle()
  36.   setTopicButtons()
  37.   showTopicText()
  38.   updateStage()
  39. end
  40.  
  41. on doSearch input
  42.   if input = EMPTY then
  43.     exit
  44.   end if
  45.   if (the number of words in input = 1) and not ignorableWord(word 1 of input) then
  46.     set searchResults to searchOneTopic(input)
  47.     return searchResults
  48.   end if
  49.   if (the number of words in input > 1) and containsIgnorableWords(input) then
  50.     set newInput to removeIgnorableWords(input)
  51.     set searchResults to doSearch(newInput)
  52.     return searchResults
  53.   end if
  54.   if (the number of words in input > 1) and not containsIgnorableWords(input) then
  55.     set containingList to []
  56.     set numTopics to the number of words in input
  57.     repeat with i = 1 to numTopics
  58.       set indexCode to word i of input && "INDEX"
  59.       if the number of cast indexCode <> -1 then
  60.         set topicsContainingCurrentTopic to the text of cast indexCode
  61.         repeat with j = 1 to the number of lines in topicsContainingCurrentTopic
  62.           add(containingList, line j of topicsContainingCurrentTopic)
  63.         end repeat
  64.       end if
  65.     end repeat
  66.     sort(containingList)
  67.     set searchResults to EMPTY
  68.     repeat with i = 1 to count(containingList) - numTopics + 1
  69.       set currentTopic to getAt(containingList, i)
  70.       set containsAll to 1
  71.       repeat with j = 1 to numTopics - 1
  72.         if not (getAt(containingList, i + j) = currentTopic) then
  73.           set containsAll to 0
  74.           exit repeat
  75.         end if
  76.       end repeat
  77.       if containsAll = 1 then
  78.         put currentTopic & RETURN after searchResults
  79.       end if
  80.     end repeat
  81.     return searchResults
  82.   end if
  83. end
  84.  
  85. on ignorableWord whichWord
  86.   if (whichWord = "and") or (whichWord = "of") or (whichWord = "the") or (whichWord = "a") or (whichWord = "an") then
  87.     return 1
  88.   else
  89.     return 0
  90.   end if
  91. end
  92.  
  93. on searchOneTopic topic
  94.   set firstLetter to char 1 of topic
  95.   set letterNum to 1
  96.   set letterField to firstLetter && letterNum && "INDEX"
  97.   set indexFound to 0
  98.   repeat while the number of cast letterField <> -1
  99.     set letterIndex to field letterField
  100.     set topicOffset to offset("*" & topic, letterIndex)
  101.     if topicOffset = 0 then
  102.       set letterNum to letterNum + 1
  103.       set letterField to firstLetter && letterNum && "INDEX"
  104.       next repeat
  105.     end if
  106.     set indexFound to 1
  107.     exit repeat
  108.   end repeat
  109.   if indexFound then
  110.     set topicsFromUserInput to char topicOffset + 1 to 32000 of letterIndex
  111.     set nextTopicOffset to offset("*", topicsFromUserInput)
  112.     set searchResults to char 1 to nextTopicOffset - 2 of topicsFromUserInput
  113.     set numLines to the number of lines in searchResults
  114.     set searchResults to line 2 to numLines of searchResults
  115.     return searchResults
  116.   else
  117.   end if
  118. end
  119.  
  120. on containsIgnorableWords phrase
  121.   repeat with i = 1 to the number of words in phrase
  122.     if ignorableWord(word i of phrase) then
  123.       return 1
  124.     end if
  125.   end repeat
  126.   return 0
  127. end
  128.  
  129. on removeIgnorableWords phrase
  130.   set newPhrase to EMPTY
  131.   repeat with i = 1 to the number of words in phrase
  132.     if not ignorableWord(word i of phrase) then
  133.       set newPhrase to addWordToString(word i of phrase, newPhrase)
  134.     end if
  135.   end repeat
  136.   return newPhrase
  137. end
  138.  
  139. on addWordToString whichWord, phrase
  140.   if phrase = EMPTY then
  141.     return whichWord
  142.   else
  143.     return phrase && whichWord
  144.   end if
  145. end
  146.  
  147. on setSearchSuccessFul val
  148.   global searchSuccessFul
  149.   set searchSuccessFul to val
  150. end
  151.